package com.thermal.thermal_inspection.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.thermal.thermal_inspection.entity.User;
import com.thermal.thermal_inspection.mapper.*;
import com.thermal.thermal_inspection.service.AccountService;
import com.thermal.thermal_inspection.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    UserMapper userMapper;


    /**
     * 登录
     */
    @Override
    public User login(String username, String password){
        //先查找用户是否存在
        User user = userMapper.existUser(username, password);
        if(user == null){
            return null;
        }
        return user;
    }

    /**
     * 注册
     */
    @Override
    public int register(User user){
        //先检查是否有重复(检查user表，username不能相同)
        User user1 = userMapper.getUserByUsername(user.getUsername());
        if(user1 != null){
            return 0;
        }
        userMapper.register(user);
        return 1;
    }
    /**
     * 修改信息
     */
    @Override
    public int fixAccount(User user){
        //先判断工人是否存在
        User user1 = userMapper.getUserByUserId(user.getUserId());
        if(user1 == null){
            System.out.println("修改信息失败，该用户不存在");
            return 0;
        }

        //然后判断用户名是否已存在（除开自己）
        User user2 = userMapper.getUserByUsername(user.getUsername());
        if(user2 != null && user1.getUserId() != user2.getUserId()){
            System.out.println("修改信息失败，用户名已存在，请重新填写用户名");
            return 0;
        }
        userMapper.fix(user);
        return 1;
    }

    /**
     * 查找个人信息
     */
    @Override
    public User getUserInfo(int userId){
        //先判断工人是否存在
        User user1 = userMapper.getUserByUserId(userId);
        if(user1 == null){
            return null;
        }

        return user1;
    }
}
